package base.web.tools;

import base.service.domain.exception.NoSuchBeanException;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 日期:2011-1-17<br/>
 * 描述:从spring中获取bean对象的辅助工具类<br/>
 * <p/>
 * 历史记录:
 * <ol>
 * 日期:2011-1-17 作者:yanghongjian
 * <li>创建功能代码</li>
 * </ol>
 * 
 * <a href="mailto:12719889@qq.com">YangHongJian</a>
 * @version 2.0
 * @since 1.6
 */
public final class BeansHelp {
	private static Logger log = Logger.getLogger(BeansHelp.class);
	private static ApplicationContext ctx;
	private static WebApplicationContext webCtx;

	protected BeansHelp() {
	}

	static {
		showInfo();

		if (ctx == null) {
            try{
			webCtx = WebApplicationContextUtils
					.getRequiredWebApplicationContext(ServletActionContext
							.getServletContext());
                log.info("web.xml加载spring");
            }catch(Exception e){
                log.info("直接加载spring");
            }
			if (webCtx == null)
				ctx = new ClassPathXmlApplicationContext("spring-config.xml");
		}
	}

	/**
	 * 显示系统一些版权信息
	 */
	public static void showInfo() {
		StringBuffer sb = new StringBuffer();
		sb.append("\n\t\t\t\t产品平台 Ver2.0 Date:2011.7.1\n")
				.append("\t----------------------------------------------\n")
				.append("\t\t部署环境:\n")
				.append("\t\t\t中文环境(unix/liunx)设置 export lang=zh_CN\n")
				.append("\t\t\tJDK1.5.x servlet2.4 jsp2.0\n")
				.append("\t\t系统架构:\n")
				.append("\t\t\tweb层:struts2-core-j4-2.0.11.2 jQuery1.6 页面字符集:UTF-8\n")
				.append("\t\t\tdao层:ibatis-2.3.0.677\n")
				.append("\t\t\tbo层:Spring3.0\n").append("\t\t\t数据库字符集:UTF-8\n")
				.append("\t\t\t\t\t\t\t\t*产品组*\n\n")
				.append("\t\t\t\t郑州新益华医学科技有限公司\n")
				.append("\t----------------------------------------------\n");
		log.info(sb.toString());
	}

	/**
	 * 通过beanID获取bean实例
	 * 
	 * @param beanID
	 *            bean的代码
	 * @return 返回对应的实例
	 * @throws base.service.domain.exception.NoSuchBeanException
	 *             bean没有定义
	 */
	public static Object getBeanInstance(String beanID)
			throws NoSuchBeanException {
		log.info("在spring容器中获取Bean对象 ID=" + beanID);
		Object obj;
		if (BeansHelp.ctx == null) {
			if (BeansHelp.webCtx.containsBeanDefinition(beanID)) {
				obj = BeansHelp.webCtx.getBean(beanID);
			} else {
				log.info("beanID=" + beanID + "没有定义");
				throw new NoSuchBeanException(beanID + "没有定义!");
			}
		} else {
			if (BeansHelp.ctx.containsBeanDefinition(beanID)) {
				obj = BeansHelp.ctx.getBean(beanID);
			} else {
				log.info("beanID=" + beanID + "没有定义");
				throw new NoSuchBeanException(beanID + "没有定义!");
			}
		}
		// if (BeansHelp.ctx.containsBeanDefinition(beanID)) {
		// obj = BeansHelp.ctx.getBean(beanID);
		// } else {
		// log.info("beanID=" + beanID + "没有定义");
		// throw new NoSuchBeanException(beanID + "没有定义!");
		// }
		return obj;
	}
}
